package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.CategoryDTO;
import com.movie.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categories")
@RequiredArgsConstructor
@Tag(name = "分类管理", description = "影片分类的增删改查接口")
public class CategoryController {
    
    private final CategoryService categoryService;
    
    @GetMapping
    @Operation(summary = "获取所有分类", description = "获取系统中所有的影片分类列表")
    public ApiResponse<List<CategoryDTO>> getAllCategories() {
        List<CategoryDTO> categories = categoryService.findAll();
        return ApiResponse.success(categories);
    }
    
    @GetMapping("/active")
    @Operation(summary = "获取启用分类", description = "获取所有状态为启用的分类列表")
    public ApiResponse<List<CategoryDTO>> getActiveCategories() {
        List<CategoryDTO> categories = categoryService.findActiveCategories();
        return ApiResponse.success(categories);
    }
    
    @GetMapping("/type/{type}")
    @Operation(summary = "按类型获取分类", description = "根据分类类型获取分类列表")
    public ApiResponse<List<CategoryDTO>> getCategoriesByType(
            @Parameter(description = "分类类型: movie-影片分类, news-资讯分类") @PathVariable String type) {
        List<CategoryDTO> categories = categoryService.findByType(type);
        return ApiResponse.success(categories);
    }
    
    @GetMapping("/active/type/{type}")
    @Operation(summary = "按类型获取启用分类", description = "根据分类类型获取状态为启用的分类列表")
    public ApiResponse<List<CategoryDTO>> getActiveCategoriesByType(
            @Parameter(description = "分类类型: movie-影片分类, news-资讯分类") @PathVariable String type) {
        List<CategoryDTO> categories = categoryService.findActiveCategoriesByType(type);
        return ApiResponse.success(categories);
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取分类", description = "根据分类ID获取具体的分类信息")
    public ApiResponse<CategoryDTO> getCategoryById(
            @Parameter(description = "分类ID") @PathVariable Long id) {
        CategoryDTO category = categoryService.findById(id);
        return ApiResponse.success(category);
    }
    
    @GetMapping("/search")
    @Operation(summary = "搜索分类", description = "根据关键词搜索分类")
    public ApiResponse<List<CategoryDTO>> searchCategories(
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "分类类型") @RequestParam(required = false) String type) {
        List<CategoryDTO> categories = categoryService.searchCategories(keyword, type);
        return ApiResponse.success(categories);
    }
    
    @PostMapping
    @Operation(summary = "创建分类", description = "创建新的影片分类")
    public ApiResponse<CategoryDTO> createCategory(
            @Parameter(description = "分类信息") @RequestBody CategoryDTO categoryDTO) {
        CategoryDTO created = categoryService.create(categoryDTO);
        return ApiResponse.success("分类创建成功", created);
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新分类", description = "根据ID更新分类信息")
    public ApiResponse<CategoryDTO> updateCategory(
            @Parameter(description = "分类ID") @PathVariable Long id, 
            @Parameter(description = "分类信息") @RequestBody CategoryDTO categoryDTO) {
        CategoryDTO updated = categoryService.update(id, categoryDTO);
        return ApiResponse.success("分类更新成功", updated);
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类", description = "根据ID删除分类，如果分类下有影片则无法删除")
    public ApiResponse<Void> deleteCategory(
            @Parameter(description = "分类ID") @PathVariable Long id) {
        categoryService.delete(id);
        return ApiResponse.success("分类删除成功", null);
    }
}